package com.java110.mall.chain.partner.adapter.hibo.utils;

import com.alibaba.fastjson.JSONObject;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.TreeMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HiboService {
    private static Logger logger = LoggerFactory.getLogger(HiboService.class);

    public HiboService() {
    }

    public static <T> Response<T> call(String url, String appId, String body, String secret, Type typeOfT)  {
        checkParam(url, appId, body, secret);
        TreeMap<String, String> param = new TreeMap();
        param.put("appId", appId);
        param.put("body", body);
        param.put("timestamp", String.valueOf(System.currentTimeMillis()));
        param.put("sign", SignUtils.sign(param, secret));

        try {
            if (logger.isDebugEnabled()) {
                logger.debug("海博erp平台client-call-hibo:url={},param={}", url, JSONObject.toJSONString(param));
            }

            HttpResponse responseEntity = HttpUtils.post(url, param);
            if (responseEntity != null && responseEntity.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = responseEntity.getEntity();
                String result = EntityUtils.toString(resEntity, StandardCharsets.UTF_8);
                if (StringUtils.isBlank(result)) {
                    logger.error("http-post:url={}，接口响应为信息为空", url);
                    throw new IllegalArgumentException("接口返回信息为空");
                } else {
                    return (Response)JSONObject.parseObject(result, typeOfT);
                }
            } else {
                logger.debug("http-post:url={},response={}", url, JSONObject.toJSONString(responseEntity));
                throw new IllegalArgumentException("接口返回为空");
            }
        } catch (Exception var9) {
            logger.error("海博erp平台client-call-hibo:url={},ex={}", new Object[]{url, var9.getMessage(), var9});
            return null;
        }
    }

    public static <T> T call(String url, String appId, String body, String secret, Class<T> clazz) {
        checkParam(url, appId, body, secret);
        TreeMap<String, String> param = new TreeMap();
        param.put("appId", appId);
        param.put("body", body);
        param.put("timestamp", String.valueOf(System.currentTimeMillis()));
        param.put("sign", SignUtils.sign(param, secret));

        try {
            if (logger.isDebugEnabled()) {
                logger.debug("海博erp平台client-call-hibo:url={},param={}", url, JSONObject.toJSONString(param));
            }

            HttpResponse responseEntity = HttpUtils.post(url, param);
            if (responseEntity != null && responseEntity.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = responseEntity.getEntity();
                String result = EntityUtils.toString(resEntity, StandardCharsets.UTF_8);
                if (StringUtils.isBlank(result)) {
                    logger.error("http-post:url={}，接口响应为信息为空", url);
                    throw new IllegalArgumentException("接口返回信息为空");
                } else {
                    return JSONObject.parseObject(result, clazz);
                }
            } else {
                logger.debug("http-post:url={},response={}", url, responseEntity);
                throw new IllegalArgumentException("接口返回为空");
            }
        } catch (Exception var9) {
            logger.error("海博erp平台client-call-hibo:url={},ex={}", new Object[]{url, var9.getMessage(), var9});
            return null;
        }
    }

    private static void checkParam(String url, String appId, String body, String secret) {
        if (StringUtils.isBlank(url)) {
            throw new IllegalArgumentException("调用海博erp服务：请求地址[url]为空");
        } else if (StringUtils.isBlank(appId)) {
            throw new IllegalArgumentException("调用海博erp服务：应用ID[appId]为空");
        } else if (StringUtils.isBlank(body)) {
            throw new IllegalArgumentException("调用海博erp服务：业务参数[body]为空");
        } else if (StringUtils.isBlank(secret)) {
            throw new IllegalArgumentException("调用海博erp服务：密钥[secret]为空");
        }
    }
}